package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.base.util.DateUtil;
import com.coding.dao.ClasswithDao;
import com.coding.dao.TeacherDao;
import com.coding.dao.TheclassDao;
import com.coding.dao.UserDao;
import com.coding.pojo.Classwith;
import com.coding.pojo.Teacher;
import com.coding.pojo.Theclass;
import com.coding.pojo.User;
import com.coding.service.ClasswithService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (Classwith)表服务实现类
 *
 * @author makejava
 * @since 2020-07-31 14:37:49
 */
@Service
public class ClasswithServiceImpl implements ClasswithService {
    @Autowired
    ClasswithDao classwithDao;
    @Autowired
    UserDao userDao;
    @Autowired
    TheclassDao theclassDao;
    @Autowired
    TeacherDao teacherDao;

    @Override
    public void queryallbyuser_id(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.selectById(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        Classwith classwith=new Classwith();
        classwith.setUserId(user_id);
        List<Classwith> classwithList=classwithDao.queryAllWithImage(classwith);
        PageInfo pageInfo = new PageInfo(classwithList);
        classwithList = pageInfo.getList();
        if (classwithList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的班级");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的班级共" + classwithList.size() + "个", classwithList);
    }

    @Override
    public void queryAllByUserId(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        Teacher teacher=teacherDao.selectByUserId(user_id);
        if (teacher==null){
            baseModel.setFailure("该用户不是老师");
            return;
        }

        Theclass theclass=new Theclass();
        theclass.setClassTeacherId(teacher.getTeacher_id());
        List<Theclass> theClassList=theclassDao.queryByTeacherId(teacher.getTeacher_id());
        if (theClassList==null||theClassList.size()==0){
            baseModel.setFailure("该用户没有管理班级");
            return;
        }
        List<Classwith> classwithList=new ArrayList<>();
        for (Theclass theclass1:theClassList
             ) {
            Classwith classwith=new Classwith();
            classwith.setTheclass(theclass1);
            classwithList.add(classwith);
        }
        if (classwithList.size()==0){
            baseModel.setFailure("查询班级错误",classwithList);
            return;
        }
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        PageInfo pageInfo = new PageInfo(classwithList);
        classwithList = pageInfo.getList();
        if (classwithList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的班级");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的班级共" + classwithList.size() + "个", classwithList);
    }

    @Override
    public void joinclass(int user_id, int class_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Theclass theclass=theclassDao.queryById(class_id);
        if (theclass == null) {
            baseModel.setFailure("该班级不存在，请检查账号是否正确");
            return;
        }
        Classwith newclass=classwithDao.queryById(user_id,class_id);
        if (newclass!=null){
            baseModel.setFailure("该用户已加入该班级");
            return;
        }
        Classwith classwith=new Classwith();
        classwith.setUserId(user_id);
        classwith.setClassId(class_id);
        classwith.setClasswithTime(DateUtil.getStringDate());
        if (classwithDao.insertSelective(classwith)==1){
            baseModel.setSucceed("用户"+user_id+"成功加入班级"+class_id);
        }else {
            baseModel.setFailure("用户加入班级失败");
        }

    }

    @Override
    public void quitclass(int user_id, int clas_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Theclass theclass=theclassDao.queryById(clas_id);
        if (theclass == null) {
            baseModel.setFailure("该班级不存在，请检查账号是否正确");
            return;
        }
        Classwith newclass=classwithDao.queryById(user_id,clas_id);
        if (newclass==null){
            baseModel.setFailure("该用户未加入该班级");
            return;
        }
        Classwith classwith=new Classwith();
        classwith.setUserId(user_id);
        classwith.setClassId(clas_id);
        if (classwithDao.deleteById(user_id,clas_id)==1){
            baseModel.setSucceed("用户"+user_id+"成功退出班级"+clas_id);
        }else {
            baseModel.setFailure("用户加入班级失败");
        }
    }
}